<?php

namespace App\Http\Controllers;

use App\Http\Requests\FactoryCreateRequest;
use App\Http\Resources\FactoryCollection;
use App\Http\Resources\FactoryLikeCollection;
use App\Http\Resources\FactoryResource;
use App\Models\Area;
use App\Models\City;
use App\Models\Factory;
use App\Models\FactoryLike;
use App\Models\FactoryView;
use App\Models\RentPrice;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class FactoryController extends Controller
{
    /**
     * @param Request $request
     * @return void
     */
    public function list(Request $request)
    {
        $cityId = $request->input('city_id');
        $townId=$request->input('town_id');
        $type = $request->input('type');
        $searchKey = $request->input('search_key');
        $is_hot = $request->input('is_hot');
        $is_jingxuan = $request->input('is_jingxuan');
        $price_id = $request->input('price_id');
        $area_id = $request->input('area_id');
        $rent_type_id = $request->input('rent_type_id');
        $rent_nature_id = $request->input('rent_nature_id');
        $pageSize = $request->input('page_size');
        $factory = Factory::with(['tag', 'city'])
            ->where('shenhe', 1)
            ->when($is_hot, function ($query) use ($is_hot) {
                $query->where('is_hot', $is_hot);
            })
            ->when($price_id, function ($query) use ($price_id) {
                $price = RentPrice::findOrFail($price_id);
                $query->where('price', '>=', $price->min)->where('price', '<=', $price->max);
            })
            ->when($type, function ($query) use ($type) {
                $query->where('type', $type);
            })
            ->when($cityId, function ($query) use ($cityId) {
                $query->where('city_id', $cityId);
            })
            ->when($townId, function ($query) use ($townId) {
                $query->where('town_id', $townId);
            })
            ->when($area_id, function ($query) use ($area_id) {
                $area = Area::findOrFail($area_id);
                $query->where('area', '>=', $area->min)->where('area', '<=', $area->max);
            })
            ->when($rent_type_id, function ($query) use ($rent_type_id) {
                $query->where('rent_type_id', $rent_type_id);
            })
            ->when($searchKey, function ($query) use ($searchKey) {
                $query->where('title', 'like', "%{$searchKey}%");
            })
            ->when($rent_nature_id, function ($query) use ($rent_nature_id) {
                $query->where('rent_nature_id', $rent_nature_id);
            })
            ->when($is_jingxuan, function ($query) use ($is_jingxuan) {
                $query->where('is_jingxuan', $is_jingxuan);
            })
            ->orderBy('sort','desc')
            ->orderBy('is_jingxuan','desc')
            ->orderBy('is_hot','desc')
            ->orderBy('created_at','desc')
            ->paginate($pageSize);
        return new FactoryCollection($factory);
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        $user = $request->user();
        $factory = Factory::with(['tag', 'city', 'rentType', 'rentNature'])->findOrFail($id);
        $factory->view_num = $factory->view_num + 1;
        $factory->save();
        if ($user) {
            $factoryView = FactoryView::where('factory_id', $id)->where('user_id', $user->id)->first();
            if ($factoryView) {
                $factoryView->updated_at = Carbon::now();
                $factoryView->save();
            } else {
                $factoryView = new FactoryView();
                $factoryView->factory_id = $id;
                $factoryView->user_id = $user->id;
                $factoryView->model = $factory->toArray();
                $factoryView->save();
            }
        }
        return new FactoryResource($factory);
    }

    public function viewList(Request $request)
    {
        $user = $request->user();
        $pageSize = $request->input('page_size');
        $factoryLike = FactoryView::where('user_id', $user->id)
            ->orderBy('id', 'desc')
            ->paginate($pageSize);
        return new FactoryLikeCollection($factoryLike);
    }

    public function save(FactoryCreateRequest $request)
    {
        \Log::info('请求',$request->all());
        $user = $request->user();
        $title = $request->input('title');
        $lat = $request->input('lat');
        $lng = $request->input('lng');
        $type = $request->input('type');
        $kaijian = $request->input('kaijian');
        $jinshen = $request->input('jinshen');
        $cenggao = $request->input('cenggao');
        $area = $request->input('area');
        $images = $request->input('images');
        $price = $request->input('price');
        $address = $request->input('address');
        $city_id = $request->input('city_id');
        $total_price=$request->input('total_price');
        $town_id = $request->input('town_id');
        $rent_type_id = $request->input('rent_type_id');
        $rent_nature_id = $request->input('rent_nature_id');
        $contact_mobile = $request->input('contact_mobile');
        $contact_name = $request->input('contact_name');
        $broker_mobile=$request->input('broker_mobile');
        $broker_name=$request->input('broker_name');
        $content = $request->input('content');
        $status = $request->input('status');
        $zhuanrang=$request->input('zhuanrang');
        $guige = $request->input('guige');
        $floor = $request->input('floor');
        $peitao=$request->input('peitao');
        $tag=$request->input('tag');
        if (is_string($images)){
            $images=explode(',',$images);
        }
        if ($price=='面谈'){
            $price=0;
        }
        $factory = new Factory();
        $factory->title = $title;
        $factory->logo=$images[0] ?? '';
        $factory->baidu_lat = $lat;
        $factory->baidu_lng = $lng;
        $factory->type = $type;
        $factory->area = $area;
        $factory->images = $images;
        $factory->broker_name=$broker_name;
        $factory->broker_mobile=$broker_mobile;
        $factory->price = $price;
        $factory->total_price=$total_price;
        $factory->address = $address;
        if (!$city_id){
            $city=City::find($town_id)->parent_id ?? '';
            $factory->city_id=$city;
        }else{
            $factory->city_id = $city_id;
        }
        $factory->town_id = $town_id;
        $factory->rent_type_id = $rent_type_id;
        $factory->zhuanrang=$zhuanrang;
        $factory->rent_nature_id = $rent_nature_id;
        $factory->contact_name = $contact_name;
        $factory->cenggao=$cenggao;
        $factory->jinshen=$jinshen;
        $factory->kaijian=$kaijian;
        $factory->contact_mobile = $contact_mobile;
        $factory->content = $content;
        $factory->status = $status;
        $factory->guige = $guige;
        $factory->floor = $floor;
        $factory->user_id = $user->id;
        $factory->save();
        if ($peitao&&is_string($peitao)){
            $peitao=explode(',',$peitao);
        }
        if ($tag&&is_string($tag)){
            $tag=explode(',',$tag);
        }
        $factory->peitao()->sync($peitao);
        $factory->tag()->sync($tag);
        return [
            'code' => 200,
            'message' => '请求成功',
            'data' => $factory
        ];
    }

    /**
     * 推荐浦源
     * @return void
     */
    public function recommend(Request $request)
    {
        $id = $request->input('id');
        $factory = Factory::find($id);
        $factorys = Factory::where('type', $factory->type)
            ->where('id', '!=', $id)
            ->where('city_id', $factory->city_id)
            ->where('shenhe', 1)
            ->inRandomOrder()
            ->limit(6)
            ->get();
        return new FactoryCollection($factorys);
    }
    public function peitao(Request  $request){
        return [
            'code' => 200,
            'message' => '请求成功',
            'data' => \App\Models\Peitao::all()
        ];
    }
    public function tag(Request  $request){
        return [
            'code' => 200,
            'message' => '请求成功',
            'data' => \App\Models\Tag::all()
        ];
    }
    public function floor(Request  $request){
        return [
            'code'=>200,
            'message'=>'请求成功',
            'data'=>Factory::$louceng
        ];
    }
}
